// JavaScript Document
$(document).ready(function(){
	/*列表控件*/
	$('#email_list_tt').datagrid({
		title: '邮件用户列表'
		, fit: true
		, url: 'email.php?method=GetData'
		, pagination: true /*底部分页栏*/
		, pageList: [100, 200, 500]
		, loadMsg: '正在加载数据，请稍等 ...'
		, columns:[[
				{field:'id', title:'ID序号', width:60}
				, {field:'addr', title:'邮件地址', width:150}
				, {field:'name', title:'发件人名称', width:150}
				, {field:'smtp', title:'邮件服务器地址', width:100}
				, {field:'user', title:'服务器登录名', width:100} 
				, {field:'pwd', title:'登录密码', width:100}
				, {field:'port', title:'端口号', width:50}
				, {field:'#id', title:'操作', width:100, align:'center', formatter:function(value, rowData){
					return "<a href='javascript:void(0)' onclick='DeleteEmail(" + rowData.id + ")'>删除</a>&nbsp;"
						+ "|&nbsp;<a href='javascript:void(0)' onclick='ModiEmail(" + rowData.id + ")'>修改</a>"
				}}
			]]  
		, toolbar:[{
				id:'email_list_add'
				, text: '添加新账号'
				, iconCls: 'icon-add'
				, handler: OnAddEamil
			}]
	});
	/*提交框*/
	$('#email_list_add_ff').form({type:'POST'
		, onSubmit: function() {
			return true;
		}
		, error: function(XMLHttpRequest, textStatus, errorThrown){
			top.$.messager.alert('错误', '提交添加失败:' + errorThrown, 'error');
		}
		, success: function(data){
			var msg = decodeJSON(data);
			if(msg.success){
				top.$.messager.alert('提示', msg.msg, 'info');
				$('#email_list_add_dd').dialog('close');
				$('#email_list_tt').datagrid('reload');
			} else {
				top.$.messager.alert('操作失败', msg.msg, 'error');
			}
		}
	});

});

/*添加新邮件*/
function OnAddEamil(){
	$('#email_list_add_dd').dialog({title:'添加新用户'});
	$('#email_list_add_ff').form('clear');
	$('input[name="port"]').val('25');
	$('#email_list_add_ff').attr("action", "email-action.php?type=AddEmail");
	$('#email_list_add_dd').dialog('open');
}

/*删除邮件*/
function DeleteEmail(id){
	top.$.messager.confirm('确认操作', '确实要删除id为' + id + '的记录码?', function(b){
		if(!b) return;
		$.ajax({type:'POST'
			, url:'email-action.php'
			, data:'id=' + id + '&type=DeleteEmail'
			, dataType: 'json'
			, error: function(XMLHttpRequest, textStatus, errorThrown){
				top.$.messager.alert('错误', '提交删除失败:' + errorThrown, 'error');
			}
			, success:function(msg){
				if(msg.success){
					top.$.messager.alert('删除成功', '删除成功', 'info');
					$('#email_list_tt').datagrid('reload');
				} else {
					top.$.messager.alert('删除失败', msg.msg, 'error');
				}
			}
		});
	});
}

/*添加*/
function ModiEmail(id){
	$('#email_list_add_dd').dialog({title:'修改id为' + id + '的邮件信息'});
	var data = $('#email_list_tt').datagrid('getData');
	$('#email_list_add_ff').form('clear');
	try{
		for(var i = 0; i < data.rows.length; i++){
			if(id == data.rows[i].id){
				$('input[name="addr"]').val(data.rows[i].addr);
				$('input[name="name"]').val(data.rows[i].name);
				$('input[name="smtp"]').val(data.rows[i].smtp);
				$('input[name="user"]').val(data.rows[i].user);
				$('input[name="pwd"]').val(data.rows[i].pwd);
				$('input[name="port"]').val(data.rows[i].port);
				break;
			}
		}
	} catch (e) {
		alert(e);
		return;
	}
	$('input[name="port"]').val('25');
	$('#email_list_add_ff').attr("action", "email-action.php?type=ModityEmail&id=" + id);
	$('#email_list_add_dd').dialog('open');
}

